﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlexCell;
using YArchitech.HVAC.Nozzle;
using YArchitech.LIB;

namespace YArchitech.HVAC
{
	public partial class FrmCylindricTable : System.Windows.Forms.Form
	{
		public FrmCylindricTable(DataTable DtCylindricalNozzle, DataTable DtCylindricalNozzleFormat)
		{
			this.InitializeComponent();
			this.dtCylindricalNozzle = DtCylindricalNozzle;
			this.dtCylindricalNozzleFormat = DtCylindricalNozzleFormat;
			base.Icon = (base.Icon = Resource.YArchitechIcon);
			this.initGrid();
			this.initSprinklerList();
		}

		private void initSprinklerList()
		{
			if (this.dtCylindricalNozzle != null && this.dtCylindricalNozzle.Rows.Count > 0)
			{
				foreach (object obj in this.dtCylindricalNozzle.Rows)
				{
					DataRow dataRow = (DataRow)obj;
					this.SprinklerList.Add(new SprinklerCylindricData
					{
						Speed = ((dataRow["FengLiang"] == DBNull.Value) ? "" : dataRow["FengLiang"].ToString()),
						Range = ((dataRow["SheCheng"] == DBNull.Value) ? "" : dataRow["SheCheng"].ToString()),
						Drop = ((dataRow["QiLiu"] == DBNull.Value) ? "" : dataRow["QiLiu"].ToString()),
						Format = ((dataRow["GuiGe"] == DBNull.Value) ? "" : dataRow["GuiGe"].ToString()),
						Count = ((dataRow["Count"] == DBNull.Value) ? "" : dataRow["Count"].ToString())
					});
				}
			}
			if (this.dtCylindricalNozzleFormat != null && this.dtCylindricalNozzleFormat.Rows.Count > 0)
			{
				foreach (object obj2 in this.dtCylindricalNozzleFormat.Rows)
				{
					DataRow dataRow2 = (DataRow)obj2;
					this.FormatList.Add(new FormatData
					{
						Format = ((dataRow2["GuiGe"] == DBNull.Value) ? "" : dataRow2["GuiGe"].ToString()),
						ThroatDiameter = ((dataRow2["HouKouDn"] == DBNull.Value) ? "" : dataRow2["HouKouDn"].ToString()),
						AirDiameter = ((dataRow2["FengKouDn"] == DBNull.Value) ? "" : dataRow2["FengKouDn"].ToString()),
						C = ((dataRow2["C"] == DBNull.Value) ? "" : dataRow2["C"].ToString()),
						L2 = ((dataRow2["L2"] == DBNull.Value) ? "" : dataRow2["L2"].ToString()),
						L3 = ((dataRow2["L3"] == DBNull.Value) ? "" : dataRow2["L3"].ToString()),
						L = ((dataRow2["L"] == DBNull.Value) ? "" : dataRow2["L"].ToString()),
						D = ((dataRow2["D"] == DBNull.Value) ? "" : dataRow2["D"].ToString()),
						E = ((dataRow2["E"] == DBNull.Value) ? "" : dataRow2["E"].ToString()),
						F = ((dataRow2["F"] == DBNull.Value) ? "" : dataRow2["F"].ToString()),
						B = ((dataRow2["B"] == DBNull.Value) ? "" : dataRow2["B"].ToString()),
						H = ((dataRow2["H"] == DBNull.Value) ? "" : dataRow2["H"].ToString())
					});
				}
			}
		}

		private void initGrid()
		{
			this.setGrid(this.grid1, "c2");
			this.setGrid(this.grid2, "c3");
			this.setGrid(this.grid3, "c4");
			this.setGrid(this.grid4, "c5");
		}

		private void setGrid(Grid flexGrid, string xmlName)
		{
			string fileName = Path.Combine(Product.DataLocation, xmlName + ".xml");
			flexGrid.LoadFromXML(fileName);
			flexGrid.Row(0).Visible = false;
			flexGrid.Column(0).Visible = false;
			flexGrid.AutoSize = true;
			flexGrid.Locked = true;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		public void GetSprinklerRes(string tabindex, string row, string col)
		{
			Grid grid = null;
			this.Count = tabindex;
			string tabindex2 = tabindex;
			int num;
			if (!(tabindex2 == "1"))
			{
				if (!(tabindex2 == "2"))
				{
					if (!(tabindex2 == "3"))
					{
						if (!(tabindex2 == "4"))
						{
							grid = this.grid1;
							num = 17;
						}
						else
						{
							num = 16;
							grid = this.grid4;
						}
					}
					else
					{
						num = 17;
						grid = this.grid3;
					}
				}
				else
				{
					num = 17;
					grid = this.grid2;
				}
			}
			else
			{
				num = 17;
				grid = this.grid1;
			}
			List<SprinklerCylindricData> source = (from s in this.SprinklerList
			where s.Count == tabindex
			select s).ToList<SprinklerCylindricData>();
			if (Convert.ToInt32(col) >= 2 && Convert.ToInt32(col) <= num && Convert.ToInt32(row) >= 2 && Convert.ToInt32(row) <= 10)
			{
				if (Convert.ToInt32(row) == 2)
				{
					this.SprinklerRes = (from s in source
					where s.Speed == grid.Cell(Convert.ToInt32(row), Convert.ToInt32(col)).Text
					select s).ToList<SprinklerCylindricData>();
					if (this.SprinklerRes != null && this.SprinklerRes.Count > 0)
					{
						base.Close();
						this.TabIndex = tabindex;
						this.cycol = col;
						this.cyrow = row;
					}
				}
				else
				{
					string drop = "";
					string range = "";
					if (Convert.ToInt32(row) % 2 == 1)
					{
						drop = grid.Cell(Convert.ToInt32(row) + 1, Convert.ToInt32(col)).Text;
						range = grid.Cell(Convert.ToInt32(row), Convert.ToInt32(col)).Text;
					}
					else
					{
						drop = grid.Cell(Convert.ToInt32(row), Convert.ToInt32(col)).Text;
						range = grid.Cell(Convert.ToInt32(row) - 1, Convert.ToInt32(col)).Text;
					}
					if (drop != "-" && range != "-" && this.IsDouble(drop) && this.IsDouble(range))
					{
						this.SprinklerRes = (from s in source
						where Convert.ToDouble(s.Drop) == Convert.ToDouble(drop) && Convert.ToDouble(s.Range) == Convert.ToDouble(range)
						select s).ToList<SprinklerCylindricData>();
						if (this.SprinklerRes != null && this.SprinklerRes.Count > 0)
						{
							base.Close();
							this.TabIndex = tabindex;
							this.cycol = col;
							this.cyrow = row;
						}
					}
				}
			}
			if (this.SprinklerRes != null && this.SprinklerRes.Count<SprinklerCylindricData>() > 0)
			{
				this.setCylindricalNozzles();
			}
		}

		private void grid1_DoubleClick(object Sender, EventArgs e)
		{
			this.setSprinklerRes(this.grid1, "1", 17);
		}

		private void grid2_DoubleClick(object Sender, EventArgs e)
		{
			this.setSprinklerRes(this.grid2, "2", 17);
		}

		private void grid3_DoubleClick(object Sender, EventArgs e)
		{
			this.setSprinklerRes(this.grid3, "3", 17);
		}

		private void grid4_DoubleClick(object Sender, EventArgs e)
		{
			this.setSprinklerRes(this.grid4, "4", 16);
		}

		private bool IsDouble(string num)
		{
			try
			{
				double.Parse(num);
			}
			catch
			{
				return false;
			}
			return true;
		}

		private void setSprinklerRes(Grid grid, string tabindex, int maxCol)
		{
			this.Count = tabindex;
			string value = grid.ActiveCell.Row.ToString();
			string value2 = grid.ActiveCell.Col.ToString();
			List<SprinklerCylindricData> source = (from s in this.SprinklerList
			where s.Count == tabindex
			select s).ToList<SprinklerCylindricData>();
			if (Convert.ToInt32(value2) >= 2 && Convert.ToInt32(value2) <= maxCol && Convert.ToInt32(value) >= 2 && Convert.ToInt32(value) <= 10)
			{
				if (Convert.ToInt32(value) == 2)
				{
					this.SprinklerRes = (from s in source
					where s.Speed == grid.ActiveCell.Text
					select s).ToList<SprinklerCylindricData>();
					if (this.SprinklerRes != null && this.SprinklerRes.Count > 0)
					{
						base.Close();
						this.TabIndex = tabindex;
						this.cycol = value2;
						this.cyrow = value;
					}
				}
				else
				{
					string drop = "";
					string range = "";
					if (Convert.ToInt32(value) % 2 == 1)
					{
						drop = grid.Cell(Convert.ToInt32(value) + 1, Convert.ToInt32(value2)).Text;
						range = grid.ActiveCell.Text;
					}
					else
					{
						drop = grid.ActiveCell.Text;
						range = grid.Cell(Convert.ToInt32(value) - 1, Convert.ToInt32(value2)).Text;
					}
					if (drop != "-" && range != "-" && this.IsDouble(drop) && this.IsDouble(range))
					{
						this.SprinklerRes = (from s in source
						where Convert.ToDouble(s.Drop) == Convert.ToDouble(drop) && Convert.ToDouble(s.Range) == Convert.ToDouble(range)
						select s).ToList<SprinklerCylindricData>();
						if (this.SprinklerRes != null && this.SprinklerRes.Count > 0)
						{
							base.Close();
							this.TabIndex = tabindex;
							this.cycol = value2;
							this.cyrow = value;
						}
					}
				}
			}
			if (this.SprinklerRes != null && this.SprinklerRes.Count<SprinklerCylindricData>() > 0)
			{
				this.setCylindricalNozzles();
			}
		}

		private void setCylindricalNozzles()
		{
			using (List<SprinklerCylindricData>.Enumerator enumerator = this.SprinklerRes.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SprinklerCylindricData item = enumerator.Current;
					FormatData formatData = (from s in this.FormatList
					where s.Format == item.Format
					select s).FirstOrDefault<FormatData>();
					if (formatData != null)
					{
						if ((from s in this.FormatList
						where s.Format == item.Format
						select s).FirstOrDefault<FormatData>() != null)
						{
							(from s in this.FormatList
							where s.Format == item.Format
							select s).FirstOrDefault<FormatData>().AirDiameter.ToDouble(0.0);
						}
						if ((from s in this.FormatList
						where s.Format == item.Format
						select s).FirstOrDefault<FormatData>() != null)
						{
							(from s in this.FormatList
							where s.Format == item.Format
							select s).FirstOrDefault<FormatData>().ThroatDiameter.ToDouble(0.0);
						}
						this.CylindricalNozzles.Add(new CylindricalNozzle
						{
							QiLiu = Convert.ToDouble(item.Drop),
							Count = Convert.ToInt32(item.Count),
							SheCheng = Convert.ToInt32(item.Range),
							FengLiang = Convert.ToInt32(item.Speed),
							GuiGe = Convert.ToInt32(item.Format),
							FengKouDn = formatData.AirDiameter.ToDouble(0.0),
							HouKouDn = formatData.ThroatDiameter.ToDouble(0.0),
							C = formatData.C.ToDouble(0.0),
							L2 = formatData.L2.ToDouble(0.0),
							L3 = formatData.L3.ToDouble(0.0),
							L = formatData.L.ToDouble(0.0),
							D = formatData.D.ToDouble(0.0),
							E = formatData.E.ToDouble(0.0),
							F = formatData.F.ToDouble(0.0),
							B = formatData.B.ToDouble(0.0),
							H = formatData.H.ToDouble(0.0)
						});
					}
				}
			}
		}

		private DataTable dtCylindricalNozzle;

		private DataTable dtCylindricalNozzleFormat;

		public string Count = "1";

		private List<SprinklerCylindricData> SprinklerList = new List<SprinklerCylindricData>();

		private List<SprinklerCylindricData> SprinklerRes = new List<SprinklerCylindricData>();

		private List<FormatData> FormatList = new List<FormatData>();

		public List<CylindricalNozzle> CylindricalNozzles = new List<CylindricalNozzle>();

		public string cyrow = "0";

		public string cycol = "0";

		public new string TabIndex = "0";
	}
}
